﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.BL;
using Web.Models;

namespace Web.Controllers
{
    [Authorize]
    public class GroupController : Controller
    {
       // DataModelContainer db = new DataModelContainer();
        Service service = new Service();

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public PartialViewResult Search(string q)
        {
            var groups = service.GroupSearch(q);

            return PartialView("_GroupSearchResult", groups);
        }

        [HttpGet]
        public ActionResult Details(int id)
        {
            var model = service.GetGroup(id);


            return View(model);
        }

        [HttpGet]
        public ActionResult Join(int id)
        {
            UserRepresentation user = (UserRepresentation)Session["userrep"];
            if (service.JoinGroup(user.GetId(), id))
            {
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(string name)
        {
            service.CreateGroup(name, User.Identity.Name);
            return View();
        }

        [HttpGet]
        public ActionResult JoinedGroups()
        {
            var model = service.GetUserGroups(User.Identity.Name);
            return View(model);
        }

        [HttpGet]
        public ActionResult Leave(int id)
        {
            service.LeaveGroup(id, User.Identity.Name);

            return RedirectToAction("JoinedGroups");
        }


    }
}
